<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul{
            list-style: none;
        }
        li{
            width: 100px;
            background: red;
            border: 1px solid;
        }
        .color{
            background:blue;
        }
    </style>
</head>

<body>
    <ul>
        <li><button>按钮</button></li>
        <li><button>按钮</button></li>
        <li><button>按钮</button></li>
        <li><button>按钮</button></li>
    </ul>
</body>
<script>
    //得到一个nodelist对象 里面有4个button
    var button_list = document.querySelectorAll('button');
    // 遍历每个button并且绑定点击事件
    button_list.forEach(function(ele,index){
        //为每个按钮配置一个单独的a
        var a = true;
        ele.onclick=function(){
            if(a){
                this.parentElement.classList.add('color');
            }else{
                this.parentElement.classList.remove('color');
            }
            a=!a;
        };
        
    });
</script>
</html>